package com.inei.eea2014.model.dao;

import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.inei.eea2014.model.connection.UtilDB;
import com.inei.eea2014.model.entity.Capitulo1;

public class Capitulo1Dao implements ICRUD<Capitulo1> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public Capitulo1Dao(Context context) {
		util = new UtilDB(context);
	}

	// metodos del CRUD
	@Override
	public Capitulo1 buscar(Capitulo1 o) {
		Capitulo1 obj = null;
		db = util.getReadableDatabase();

		cursor = db.rawQuery(
				"select * from Capitulo1 where RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (cursor.moveToFirst()) {
			obj = new Capitulo1();
			obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
			obj.setUSUARIO(cursor.getString(cursor.getColumnIndex("USUARIO")));
			obj.setC0001_001_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_001_01_T")));
			obj.setC0001_002_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_002_01_T")));
			obj.setC0001_003_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_003_01_T")));
			obj.setC0001_004_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_004_01_T")));
			obj.setC0001_005_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_005_01_T")));
			obj.setC0001_006_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_006_01_T")));
			obj.setC0001_007_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_007_01_T")));
			obj.setC0001_008_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_008_01_T")));
			obj.setC0001_009_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_009_01_T")));
			obj.setC0001_010_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_010_01_T")));
			obj.setC0001_011_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_011_01_T")));
			obj.setC0001_012_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_012_01_T")));
			obj.setC0001_013_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_013_01_T")));
			obj.setC0001_014_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_014_01_T")));
			obj.setC0001_015_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_015_01_T")));
			obj.setC0001_016_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_016_01_T")));
			obj.setC0001_017_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_017_01_T")));
			obj.setC0001_018_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_018_01_T")));
			obj.setC0001_019_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_019_01_T")));
			obj.setC0001_020_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_020_01_T")));
			obj.setC0001_021_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_021_01_T")));
			obj.setC0001_022_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_022_01_T")));
			obj.setC0001_023_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_023_01_T")));
			obj.setC0001_024_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_024_01_T")));
			obj.setC0001_029_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_029_01_T")));
			obj.setC0001_031_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_031_01_T")));
			obj.setC0001_032_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_032_01_T")));
			obj.setC0001_033_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_033_01_T")));
			obj.setC0001_034_01_T(cursor.getString(cursor
					.getColumnIndex("C0001_034_01_T")));
		}

		Cerrar();
		return obj;
	}

	@Override
	public List<Capitulo1> leer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int crear(Capitulo1 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("RUC", o.getRUC());
		valores.put("USUARIO", o.getUSUARIO());
		valores.put("C0001_001_01_T", o.getC0001_001_01_T());
		valores.put("C0001_002_01_T", o.getC0001_002_01_T());
		valores.put("C0001_003_01_T", o.getC0001_003_01_T());
		valores.put("C0001_004_01_T", o.getC0001_004_01_T());
		valores.put("C0001_005_01_T", o.getC0001_005_01_T());
		valores.put("C0001_006_01_T", o.getC0001_006_01_T());
		valores.put("C0001_007_01_T", o.getC0001_007_01_T());
		valores.put("C0001_008_01_T", o.getC0001_008_01_T());
		valores.put("C0001_009_01_T", o.getC0001_009_01_T());
		valores.put("C0001_010_01_T", o.getC0001_010_01_T());
		valores.put("C0001_011_01_T", o.getC0001_011_01_T());
		valores.put("C0001_012_01_T", o.getC0001_012_01_T());
		valores.put("C0001_013_01_T", o.getC0001_013_01_T());
		valores.put("C0001_014_01_T", o.getC0001_014_01_T());
		valores.put("C0001_015_01_T", o.getC0001_015_01_T());
		valores.put("C0001_016_01_T", o.getC0001_016_01_T());
		valores.put("C0001_017_01_T", o.getC0001_017_01_T());
		valores.put("C0001_018_01_T", o.getC0001_018_01_T());
		valores.put("C0001_019_01_T", o.getC0001_019_01_T());
		valores.put("C0001_020_01_T", o.getC0001_020_01_T());
		valores.put("C0001_021_01_T", o.getC0001_021_01_T());
		valores.put("C0001_022_01_T", o.getC0001_022_01_T());
		valores.put("C0001_023_01_T", o.getC0001_023_01_T());
		valores.put("C0001_024_01_T", o.getC0001_024_01_T());
		valores.put("C0001_029_01_T", o.getC0001_029_01_T());
		valores.put("C0001_031_01_T", o.getC0001_031_01_T());
		valores.put("C0001_032_01_T", o.getC0001_032_01_T());
		valores.put("C0001_033_01_T", o.getC0001_033_01_T());
		valores.put("C0001_034_01_T", o.getC0001_034_01_T());

		resultado = (int) db.insert("Capitulo1", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int actualizar(Capitulo1 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("C0001_001_01_T", o.getC0001_001_01_T());
		valores.put("C0001_002_01_T", o.getC0001_002_01_T());
		valores.put("C0001_003_01_T", o.getC0001_003_01_T());
		valores.put("C0001_004_01_T", o.getC0001_004_01_T());
		valores.put("C0001_005_01_T", o.getC0001_005_01_T());
		valores.put("C0001_006_01_T", o.getC0001_006_01_T());
		valores.put("C0001_007_01_T", o.getC0001_007_01_T());
		valores.put("C0001_008_01_T", o.getC0001_008_01_T());
		valores.put("C0001_009_01_T", o.getC0001_009_01_T());
		valores.put("C0001_010_01_T", o.getC0001_010_01_T());
		valores.put("C0001_011_01_T", o.getC0001_011_01_T());
		valores.put("C0001_012_01_T", o.getC0001_012_01_T());
		valores.put("C0001_013_01_T", o.getC0001_013_01_T());
		valores.put("C0001_014_01_T", o.getC0001_014_01_T());
		valores.put("C0001_015_01_T", o.getC0001_015_01_T());
		valores.put("C0001_016_01_T", o.getC0001_016_01_T());
		valores.put("C0001_017_01_T", o.getC0001_017_01_T());
		valores.put("C0001_018_01_T", o.getC0001_018_01_T());
		valores.put("C0001_019_01_T", o.getC0001_019_01_T());
		valores.put("C0001_020_01_T", o.getC0001_020_01_T());
		valores.put("C0001_021_01_T", o.getC0001_021_01_T());
		valores.put("C0001_022_01_T", o.getC0001_022_01_T());
		valores.put("C0001_023_01_T", o.getC0001_023_01_T());
		valores.put("C0001_024_01_T", o.getC0001_024_01_T());
		valores.put("C0001_029_01_T", o.getC0001_029_01_T());
		valores.put("C0001_031_01_T", o.getC0001_031_01_T());
		valores.put("C0001_032_01_T", o.getC0001_032_01_T());
		valores.put("C0001_033_01_T", o.getC0001_033_01_T());
		valores.put("C0001_034_01_T", o.getC0001_034_01_T());

		resultado = (int) db.update("Capitulo1", valores,
				"RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Capitulo1 o) {
		// TODO Auto-generated method stub
		return 0;
	}

	// otros metodos
	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
